package zhoug.common.view;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import zhoug.logging.Logger;

import androidx.appcompat.widget.AppCompatImageView;

/**
 * 支持放大缩小
 * @Author: zhoug
 * @Date: 2023/1/12
 * @Description:
 */
public class ZImageView extends AppCompatImageView {
    private static final String TAG = ">>>ZImageView";
    // 手势帮助类
    private GestureDetector mGestureDetector;
    //缩放帮助类
    private ScaleGestureDetector mScaleGestureDetector;

    public ZImageView(Context context) {
        this(context, null);
    }

    public ZImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(context, mSimpleOnGestureListener);
        mScaleGestureDetector = new ScaleGestureDetector(context, mOnScaleGestureListener);
    }

    private float mPreScaleFactor = 1.0f;
    private Matrix mMatrix;
    /**
     * scale_x   skew_x     trans_x
     * skew_y    scale_y    trans_y
     * persp_0   persp_1    persp_2
     */
    float[] mMatrixValues = new float[9];

    // 处理双指的缩放
    private ScaleGestureDetector.OnScaleGestureListener mOnScaleGestureListener = new ScaleGestureDetector.OnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            /*if (null == getDrawable() || mMatrix == null) {
                // 如果返回true那么detector就会重置缩放事件
                return true;
            }
            // 缩放因子,缩小小于1,放大大于1,是相对于开始缩放时的大小
            float scaleFactor = mScaleGestureDetector.getScaleFactor();
            Logger.d(TAG, "onScale:scaleFactor=" + scaleFactor);
            // 缩放因子偏移量 大于0放大,小于0缩小
            float deltaFactor = scaleFactor - mPreScaleFactor;
            Logger.d(TAG, "onScale:scaleFactor=" + scaleFactor + ",deltaFactor=" + deltaFactor);

            if (scaleFactor != 1.0F && deltaFactor != 0F) {
                mMatrix.getValues(mMatrixValues);
                float curScaleX = _getScale();
                //放大
                if (deltaFactor > 0) {
                    Logger.d(TAG, "onScale:放大");
                    if (curScaleX < mMaxScale) {
                        if (curScaleX * (deltaFactor + 1F) > mMaxScale) {
                            deltaFactor = mMaxScale / curScaleX - 1;
                        }
                    } else {
                        deltaFactor = 0F;
                    }
                } else {
                    //缩小
                    Logger.d(TAG, "onScale:缩小");

                    if (curScaleX > minScale) {
                        if (curScaleX * (deltaFactor + 1F) < minScale) {
                            deltaFactor = minScale / curScaleX - 1;
                        }
                    } else {
                        deltaFactor = 0F;
                    }
                }
                if (deltaFactor != 0F) {
                    mMatrix.postScale(deltaFactor + 1F, deltaFactor + 1F, mLastFocusX = mScaleGestureDetector.getFocusX(),
                            mLastFocusY = mScaleGestureDetector.getFocusY());
                    setImageMatrix(mMatrix);
                }
            }
            mPreScaleFactor = scaleFactor;*/
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            // 注意返回true
            Logger.d(TAG, "onScaleBegin:开始缩放");
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            Logger.d(TAG, "onScaleEnd:缩放结束");
        }
    };

    // 处理手指滑动
    private GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (e1.getPointerCount() == e2.getPointerCount() && e1.getPointerCount() == 1) {

                return true;
            }
            return false;
        }
    };


}
